package cn.kgc.rbac.service.impl;

import cn.kgc.rbac.common.CodeMsg;
import cn.kgc.rbac.common.ResponseResult;
import cn.kgc.rbac.entity.RolePers;
import cn.kgc.rbac.mapper.RolePersMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.kgc.rbac.entity.Permission;
import cn.kgc.rbac.service.PermissionService;
import cn.kgc.rbac.mapper.PermissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService{

    @Autowired(required = false)
    private PermissionMapper permissionMapper;

    @Autowired(required = false)
    private RolePersMapper rolePersMapper;
    @Override
    public ResponseResult deleteById(Integer id) {

        //查询删除的权限是否含有子选项
        LambdaQueryWrapper<Permission> lambda = new QueryWrapper<Permission>().lambda();
        lambda.eq(Permission::getParentId,id);

        List<Permission> permissions = permissionMapper.selectList(lambda);

        if(permissions.size()>0){
            return new ResponseResult(CodeMsg.PERMISSION_USED);
        }
        // 删除当前权限
        permissionMapper.deleteById(id);

        // 删除角色权限关系表中的相关数据
        LambdaQueryWrapper<RolePers> lambda1 = new QueryWrapper<RolePers>().lambda();
        lambda1.eq(RolePers::getPerId,id);
        rolePersMapper.delete(lambda1);

        return new ResponseResult(CodeMsg.SUCCESS);
    }
}




